/**
 * @Author: LiMeng
 * @Date: 2017/4/24
 */
var GLOBAL = {
    ACTION_URL:''
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorAuditName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
};
var getSelectedUser = function (data) {
    data = data[0];

    $('#applicantName').textbox('setValue', data.realName);
    $('#applicantId').val(data.id);
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
var toggleFieldDisableState = function (state) {
    $('#contractorAuditName').textbox(state);
    $('#exposureStartDate').datebox(state);
    $('#exposureEndDate').datebox(state);
    $('#applicantDate').datebox(state);
    $('#applicantName').textbox(state);
    $('#exposureReason').textbox(state);
    $('#punishContent').textbox(state);
};
var hideButtons = function () {
    $('.menu-right').addClass('hide');
};
var setElementWithState = function () {
    var state = $.getQueryParam('state');
    if (state === 'view') {
        toggleFieldDisableState('disable');
        hideButtons();
    }
    if (state === 'audit'){
        toggleFieldDisableState('disable');
        $('#btn_submit').addClass('hide');
        GLOBAL.ACTION_URL = ROOT_PATH + '/contractor/contractorExposure/audit.shtml';
    }
    if (state === 'edit'){
        GLOBAL.ACTION_URL = ROOT_PATH + '/contractor/contractorExposure/edit.shtml';
    }
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            var businessId = $.getQueryParam('businessId');
            if (businessId === null) {
                $.getIframeWindow().$('#dg1').datagrid('load');
            }
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
$(function () {
    $('#contractorAuditName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['700px', '400px'],
                    content: ROOT_PATH + '/contractor/contractorAudit/auditedList.shtml',
                    callBack: getSelectedContractorAudit
                });
            }
        }],
        editable: false
    });

    $('#applicantName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择提请人', getSelectedUser);
            }
        }],
        editable: false
    });

    setElementWithState();

    $('#btn_save').on('click', function () {
        $.messager.progress();	// 显示进度条

        $('#form1').form('submit', {
            url: GLOBAL.ACTION_URL,
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                ajaxSuccessHandler(result);
            }
        });
    });
    $('#btn_submit').on('click', function () {
        WindowManager.openLayer({
            type: 2,
            title: '选择下一环节处理人',
            area: ['370px', '180px'],
            content: ROOT_PATH + '/workflow/flowManagement/toFlowNextUserSelectPage.shtml',
            callBack: function (data) {//窗口关闭后的回调函数
                //得到选择的人员
                if (typeof data === 'undefined' || data === null) {
                    layer.alert("请选择处理人!");
                    return false;
                }
                $.messager.progress();	// 显示进度条

                $('#form1').form('submit', {
                    url: ROOT_PATH + '/contractor/contractorExposure/submit.shtml',
                    onSubmit: function (param) {
                        param.nextUserId = data;
                        var isValid = $(this).form('validate');
                        if (!isValid) {
                            $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                        }
                        return isValid;	// 返回false终止表单提交
                    },
                    success: function (result) {
                        ajaxSuccessHandler(result);
                    }
                });
            }
        });
    });
});